package com.shushan.util;

import android.content.res.AssetFileDescriptor;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;

import com.shushan.ui.RoundProgressBar;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @ClassName: ExamAudioPlayUtil
 * @Desciption: //音频播放工具类
 * @author: zhangshihao
 * @date: 2018-06-29
 */
public class ExamAudioPlayUtil {

    private static final String TAG = ExamAudioPlayUtil.class.getSimpleName();

    private static MediaPlayer mediaPlayer;
    private static RoundProgressBar rProgressBar;
    private static AnimationDrawable oldPlayAnim;

    public static void playAudio(final RoundProgressBar pb, String path, MediaPlayer.OnCompletionListener listener){
        if(mediaPlayer == null){
            mediaPlayer = new MediaPlayer();
        }
        try {
            rProgressBar = pb;
            pb.setProgress(0);
            Log.i(TAG,"path == "+path);
            mediaPlayer.reset();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if(!mp.isPlaying()){
                        mp.start();
                    }
                }
            });
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(listener);
            int costTime = mediaPlayer.getDuration();
            pb.countBack(pb,costTime);
        }catch (IOException ioe){
            Log.d(TAG,"audio path error : "+ioe.toString());
            ioe.printStackTrace();
        }
    }

    public static void playAudio(String audioPath, final AnimationDrawable playAnim){
        if(mediaPlayer == null){
            mediaPlayer = new MediaPlayer();
        }else{
            if(mediaPlayer.isPlaying()){
                mediaPlayer.stop();
                if(oldPlayAnim != null && oldPlayAnim.isRunning()){
                    oldPlayAnim.stop();
                }
            }
        }
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(audioPath);
            mediaPlayer.prepare();
            mediaPlayer.start();
            oldPlayAnim = playAnim;

            if(playAnim != null){
                playAnim.setOneShot(false);
                playAnim.start();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        if(playAnim.isRunning()){
                            playAnim.stop();
                        }
                    }
                });
            }

        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

    public static void playAudioSection(final double begin,final double end, final RoundProgressBar pb, AssetFileDescriptor fd){
        try {
            if(mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            }
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(),
                    fd.getLength());
            mediaPlayer.prepare();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.seekTo((int)(begin*1000));
                }
            });
            mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    if(!mediaPlayer.isPlaying()){
                        mediaPlayer.start();
                        rProgressBar = pb;
                        pb.countBack(pb,(int)((end-begin)*1000));
                        Timer endTimer = new Timer();
                        endTimer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                if(mediaPlayer.isPlaying()){
                                    mediaPlayer.stop();
                                    Log.i(TAG,"timer : stop play...");
                                }
                            }
                        },(long)((end-begin)*1000));
                        Log.d(TAG,"play time == "+(long)((end-begin)*1000));
                    }
                }
            });
        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

    public static boolean isPlaying(){
        if(mediaPlayer == null){
            return false;
        }
        if(mediaPlayer.isPlaying()){
            return true;
        }
        return false;
    }

    public static void stop(){
        if(mediaPlayer != null && mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }

        if(oldPlayAnim != null && oldPlayAnim.isRunning()){
            oldPlayAnim.stop();
        }
    }

    public static void stopPlay(){
        if(mediaPlayer == null || rProgressBar == null){
            Log.i(TAG,"stop err : element is null");
            return;
        }
        if(mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            rProgressBar.stopCountBack(rProgressBar);
        }
    }

}
